package com.situation.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situation.mapper.TbDepartmentMapper;
import com.situation.pojo.DO.TbDepartment;
import com.situation.pojo.DO.TbImage;
import com.situation.pojo.DO.TbUser;
import com.situation.pojo.VO.TbDepartmentRenShuVO;
import com.situation.pojo.VO.UserDeanVO;
import com.situation.service.TbDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author TaoMinghui
 * @create 2021-12-07 9:17
 */
@Service
public class TbDepartServiceImpl implements TbDepartmentService {

    @Autowired
    private TbDepartmentMapper tbDepartmentMapper;

    /**
     * 分页查询所有部门  （带部门人数）
     * @return
     */
    @Override
    public PageInfo<TbDepartmentRenShuVO> queryAllTbDepartmentFenYeRenShu(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<TbDepartmentRenShuVO> tbDepartmentRenShuVOS = tbDepartmentMapper.queryAllTbDepartmentFenYeRenShu();
        //将没有人数的部门置为0
        for (TbDepartmentRenShuVO tbDepartmentRenShuVO : tbDepartmentRenShuVOS) {
            Long id = tbDepartmentRenShuVO.getId();
            if(tbDepartmentMapper.queryVORenShu((int)(long)id).size() == 0){
                tbDepartmentRenShuVO.setRenShu(0);
            }
        }
        PageInfo<TbDepartmentRenShuVO> tbDepartmentRenShuVOPageInfo = new PageInfo<>(tbDepartmentRenShuVOS);
        return tbDepartmentRenShuVOPageInfo;
    }


    @Override
    public List<TbDepartment> queryAllDepartment() {
        return tbDepartmentMapper.queryAllTbDepartment();
    }

    /**
     * 添加部门
     * @param tbDepartment
     * @return 0 未添加 1 添加成功
     */
    @Override
    public int addTbDepartment(TbDepartment tbDepartment) {
        tbDepartment.setCreateTime(LocalDateTime.now());
        tbDepartment.setModifiedTime(LocalDateTime.now());
        return tbDepartmentMapper.addTbDepartment(tbDepartment);
    }

    @Override
    public long findIdByName(String name) {
        return tbDepartmentMapper.findIdByName(name);
    }

    /**
     * 通过部门id删除部门
     * @param id
     * @return
     */
    @Override
    public int deleteTbDepartmentById(long id) {
        return tbDepartmentMapper.deleteTbDepartmentById(id);
    }

    /**
     * 编辑部门
     * @param tbDepartment
     * @return
     */
    @Override
    public int editTbDepartment(TbDepartment tbDepartment) {
        return tbDepartmentMapper.editTbDepartment(tbDepartment);
    }

    /**
     * 通过模糊部门名称查询部门列表
     * @param moHuName
     * @return
     */
    @Override
    public PageInfo<TbDepartmentRenShuVO> findAllTbDepartmentByMoHuName(int pageNum,int pageSize,String moHuName) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbDepartmentRenShuVO> tbDepartmentByMoHuNames = tbDepartmentMapper.findAllTbDepartmentByMoHuName(moHuName);
        //将没有人数的部门置为0
        for (TbDepartmentRenShuVO tbDepartmentRenShuVO : tbDepartmentByMoHuNames) {
            Long id = tbDepartmentRenShuVO.getId();
            if(tbDepartmentMapper.queryVORenShu((int)(long)id).size() == 0){
                tbDepartmentRenShuVO.setRenShu(0);
            }
        }

        PageInfo<TbDepartmentRenShuVO> tbDepartmentRenShuVOPageInfo = new PageInfo<>(tbDepartmentByMoHuNames);
        return tbDepartmentRenShuVOPageInfo;
    }

    @Override
    public TbDepartment findTbDepartmentById(long id) {
        return tbDepartmentMapper.findTbDepartmentById(id);
    }

//    /**
//     * 查询部门主任，并除去禁用用户
//     * @return
//     */
//    @Override
//    public List<UserDeanVO> findDeanList() {
//        return tbDepartmentMapper.findDeanList();
//    }
}
